package com.wodan.zhongjinjicang.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.wodan.zhongjinjicang.common.exception.BusinessException;
import com.wodan.zhongjinjicang.common.utils.JsonUtils;
import com.wodan.zhongjinjicang.enums.BusinessErrorEnum;
import com.wodan.zhongjinjicang.enums.YesOrNoEnum;
import com.wodan.zhongjinjicang.frame.AbstractClientApiJsonBusinessMethodService;
import com.wodan.zhongjinjicang.mapper.UserMapper;
import com.wodan.zhongjinjicang.mapper.VerificationMapper;
import com.wodan.zhongjinjicang.model.UserBean;
import com.wodan.zhongjinjicang.model.VerificationBean;
import com.wodan.zhongjinjicang.modeldto.UserDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;


/**
 * 修改用户手机
 */
@Service("editPhone")
public class UserEditPhoneService extends AbstractClientApiJsonBusinessMethodService {


    private static final String OLD_PHONE = "oldPhone";
    private static final String NEW_PHONE = "newPhone";
    private static final String RAN_DPWD = "randPwd";
    private static final String USER_ID = "id";



    @Autowired
    private UserMapper userDao;

    @Autowired
    private VerificationMapper verificationDao;


    @Override
    protected JSONObject doExecute(JSONObject data, String sessionId) {
        UserDTO userDTO = JsonUtils.jsonToPojo(data.toJSONString(), UserDTO.class);

        int recordnum = 0;
        // 验证新手机号是否已经被注册过
        if (!checkIsRegisterByPhone(userDTO.getNewPhone())) {
            // 手机号是否申请过验证码
            VerificationBean v = verificationDao.selectByPhone(userDTO.getOldPhone());
            if (v != null) {
                Calendar c = Calendar.getInstance();
                //验证码是否过期
                if (v.getExpireTime().after(c.getTime())) {
                    //验证码是否正确
                    if (v.getValidCode().equals(userDTO.getRandPwd())) {
                        recordnum = editUserPhone(userDTO);
                        //修改是否成功
                        if (recordnum > 0) {
                            return getJsonObject(userDTO);
                        }else{
                            throw new BusinessException(BusinessErrorEnum.ERROR.getCode(), "修改失败");
                        }
                    }else{
                        throw new BusinessException(BusinessErrorEnum.ERROR.getCode(), "验证码不正确");
                    }
                }else{
                    verificationDao.updateCodeStatus(YesOrNoEnum.NO.getCode(),v.getId());
                    throw new BusinessException(BusinessErrorEnum.ERROR.getCode(), "验证码已经过期");
                }
            }else{
                throw new BusinessException(BusinessErrorEnum.ERROR.getCode(), "手机未申请验证码");
            }
        }else{
            throw new BusinessException(BusinessErrorEnum.ERROR.getCode(), "新手机已经被注册");
        }

    }

    private JSONObject getJsonObject(UserDTO userDTO) {
        UserBean userBean = userDao.selectByPhone(userDTO.getNewPhone());
        userBean.setPassword("");
        Map<String, Object> modelMap = new HashMap<>();
        modelMap.put("userInfo",userBean);
        return new JSONObject(modelMap);
    }

    private boolean checkIsRegisterByPhone(String phone) {
        if(userDao.selectByPhone(phone)!=null){
            return true;
        }
        return false;
    }

    private int editUserPhone(UserDTO userDTO) {
        return userDao.updatePhoneByUserId(userDTO.getId(),userDTO.getNewPhone());
    }

    @Override
    protected boolean isNeedLogin() {
        return true;
    }

    @Override
    protected String[] getRequiredParams() {
        return new String[]{OLD_PHONE,NEW_PHONE,RAN_DPWD,USER_ID};
    }

}
